package org.example.mallweb.mallservice.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.example.mallcommon.constant.CollectConstant;
import org.example.mallcommon.constant.FavoriteConstant;
import org.example.mallcommon.projo.Collect;
import org.example.mallcommon.projo.Favorite;
import org.example.mallcommon.result.ReturnResult;
import org.example.mallweb.mallservice.mybatiscommon.favorite.CollectMapper;
import org.example.mallweb.mallservice.service.CollectService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 韩先楚
 */
@Service
public class CollectServiceImpl implements CollectService {

    @Resource
    private CollectMapper collectMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Integer isCollect(Integer userId, Long articleId) {
        //先从redis中查询是否点赞过
        if (stringRedisTemplate.opsForHash().hasKey(CollectConstant.COLLECT_REDIS_KEY + userId, articleId.toString())) {
            //如果redis查询到，就直接返回点赞状态
            Object o = stringRedisTemplate.opsForHash().get(CollectConstant.COLLECT_REDIS_KEY + userId, articleId.toString());
            return Integer.parseInt(o.toString());
        }
        //如果redis没有查询到，就查询数据库
        Collect collect = collectMapper.selectOne(new QueryWrapper<Collect>().eq("user_id", userId).eq("article_id", articleId));

        //如果数据库查不到,先插入到数据库再更新到redis
        if (collect == null) {
            Collect collect1 = new Collect();
            collect1.setArticleId(articleId);
            collect1.setUserId(userId);
            collect1.setCreateTime(new Date());
            collectMapper.insert(collect1);
            stringRedisTemplate.opsForHash().put(CollectConstant.COLLECT_REDIS_KEY + userId, articleId.toString(), "0");
            return 0;
        }
        stringRedisTemplate.opsForHash().put(CollectConstant.COLLECT_REDIS_KEY + userId, articleId.toString(), collect.getStatus().toString());
        return Integer.parseInt(collect.getStatus().toString());
    }

    /**
     * 收藏博客或取消收藏博客(status=1为收藏，status=0为取消收藏)
     */
    public ReturnResult<String> collectBlog(Integer status, Integer userId, Long articleId) {
        //直接修改redis中的状态
        if (stringRedisTemplate.opsForHash().hasKey(CollectConstant.COLLECT_REDIS_KEY + userId, articleId.toString())) {
            stringRedisTemplate.opsForHash().put(CollectConstant.COLLECT_REDIS_KEY + userId, articleId.toString(), status.toString());
            return ReturnResult.success("收藏成功");
        }
        //这里出现redis找不到是因为springtask执行了同步数据库操作,把同步完的数据删除才找不到
        //如果没有redis没有就直接修改数据库
        Collect collect = new Collect();
        collect.setStatus(status);
        collect.setUserId(userId);
        collect.setArticleId(articleId);
        int update = collectMapper.update(collect, new QueryWrapper<Collect>().eq("user_id", userId).eq("article_id", articleId));
        stringRedisTemplate.opsForHash().put(CollectConstant.COLLECT_REDIS_KEY + userId, articleId.toString(), status.toString());
        return ReturnResult.success("点赞成功");
    }
}
